home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-12-07 | 6.1 KB | 284 lines | [TEXT/KAHL] |
- /***
- *
- * DialogUtilities.cp
- *
- * Copyright © 1995, by Christopher E. Hyde. All rights reserved.
- *
- ***/
-
- #include <Icons.h>
- #include "DialogUtilities.h"
-
-
- Handle
- GetItemHandle (DialogPtr d, short item)
- {
- Rect rect;
- Handle tempH;
- short type;
-
- GetDialogItem(d, item, &type, &tempH, &rect);
- return tempH;
- }
-
-
- void
- SetItemHandle (DialogPtr d, short item, Handle handle)
- {
- Rect rect;
- Handle tempH;
- short type;
-
- GetDialogItem(d, item, &type, &tempH, &rect);
- SetDialogItem(d, item, type, handle, &rect);
- }
-
-
- void
- GetItemStr (DialogPtr d, short item, Str255 s)
- {
- GetDialogItemText(GetItemHandle(d, item), s);
- }
-
-
- void
- SetItemStr (DialogPtr d, short item, ConstStr255Param s)
- {
- SetDialogItemText(GetItemHandle(d, item), s);
- }
-
-
- short
- GetItemValue (DialogPtr d, short item)
- {
- return GetControlValue(GetItemControl(d, item));
- }
-
-
- void
- SetItemValue (DialogPtr d, short item, short value)
- {
- SetControlValue(GetItemControl(d, item), value);
- }
-
-
- bool
- GetItemHilite (DialogPtr d, short item)
- {
- return (*GetItemControl(d, item))->contrlHilite != 0xFF;
- }
-
-
- // Temporarily highlight a push button
- void
- SetItemHilite (DialogPtr d, short item, bool flag)
- {
- HiliteControl(GetItemControl(d, item), flag ? kControlButtonPart : kControlNoPart);
- }
-
-
- void
- SetItemEnable (DialogPtr d, short item, bool flag)
- {
- HiliteControl(GetItemControl(d, item), flag ? kControlNoPart : kControlInactivePart);
- }
-
-
- // Toggle a checkbox or radio button's state and return its new state
- bool
- ToggleItem (DialogPtr d, short item)
- {
- ControlHandle c = GetItemControl(d, item);
- bool newState = (GetControlValue(c) == 0);
- SetControlValue(c, newState);
- return newState;
- }
-
-
- void
- ClickItem (ControlHandle c, short part)
- {
- HiliteControl(c, part);
- long finalTicks;
- Delay(6, &finalTicks);
- HiliteControl(c, kControlNoPart);
- }
-
-
- void
- ClickItem (DialogPtr d, short item, short part)
- {
- ClickItem(GetItemControl(d, item), part);
- }
-
-
- #if 0
- // Given a dialog pointer and a button item number, this will cause the button to look as if it has been
- // clicked. This is nice to do for the user if they type return or enter to select the default item.
- void
- SelectButton (DialogPtr d, short item)
- {
- ClickItem(d, item, kControlButtonPart);
- }
-
-
- pascal void
- SetupUserItem (DialogPtr d, short item, UserItemProcPtr itemProc)
- {
- short type;
- Handle handle;
- Rect rect;
-
- GetDialogItem(d, item, &type, &handle, &rect);
- SetDialogItem(d, item, type, Handle(itemProc), &rect);
- }
- #endif
-
-
- void
- ClickItem (DialogPtr d, short item)
- {
- short type;
- Handle handle;
- Rect rect;
-
- GetDialogItem(d, item, &type, &handle, &rect);
-
- short part;
- switch (type) {
- case kButtonDialogItem: part = kControlButtonPart; break;
- case kCheckBoxDialogItem: part = kControlCheckBoxPart; break;
- case kRadioButtonDialogItem: part = kControlRadioButtonPart; break;
- default: return;
- }
- ClickItem(ControlHandle(handle), part);
- }
-
-
- void
- SetItemIcon (DialogPtr d, short item, short iconID)
- {
- Handle icon = Get1Resource('ICON', iconID);
- if (icon != nil) {
- short type;
- Handle handle;
- Rect rect;
-
- GetDialogItem(d, item, &type, &handle, &rect);
- SetDialogItem(d, item, type, icon, &rect);
-
- PlotIcon(&rect, icon);
- }
- }
-
-
- // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
- // The following enables us to put key equivalent information in the resource fork, so that they are localizable.
- // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
-
- enum {
- kComd = 1,
- kShft = 2,
- kOptn = 4,
- kCtrl = 8,
- kShiftModifiers = 8,
- kNoModifiers = 0,
- kAllModifiers = cmdKey | shiftKey | optionKey | controlKey
- };
-
- #if 0
- short pMods[] = {
- kNoModifiers, cmdKey,
- shiftKey, cmdKey | shiftKey,
- optionKey, optionKey | cmdKey,
- optionKey | shiftKey, optionKey | cmdKey | shiftKey,
- controlKey, controlKey | cmdKey,
- controlKey | shiftKey, controlKey | cmdKey | shiftKey,
- optionKey | controlKey, optionKey | controlKey | cmdKey,
- optionKey | controlKey | shiftKey, optionKey | controlKey | cmdKey | shiftKey
- };
- #endif
-
- struct TKeyEquiv {
- uchar fKey; // Character code of keyboard key
- UInt8 fItem; // Number of dialog item to click
- UInt8 fModsOn; // Modifier keys that must be on (down)
- UInt8 fModsOff; // Modifier keys that must be off (up)
- // Byte fPart; // The item part to click
- };
- typedef TKeyEquiv** TKeyEquivHandle;
-
-
- // Re-translate a keyDown event removing the effects of the control, option & command keys.
- // Call twice with SHIFT set to TRUE then FALSE to get values for both key legends.
- static long
- Modify (const EventRecord& event, bool shift)
- {
- // Assemble new key code with no modifiers (initially). Put virtual key code in low byte
- short keyCode = (event.message & keyCodeMask) >> 8;
- if (shift)
- keyCode |= shiftKey;
-
- // get pointer to current 'KCHR'
- void* KCHRPtr = (void*) GetScriptManagerVariable(smKCHRCache);
-
- // initialize KeyTranslate dead-key state see what ascii code is returned
- UInt32 someState = 0;
-
- // look for returned values in both high and low word of result
- return KeyTranslate(KCHRPtr, keyCode, &someState);
- }
-
-
- pascal bool
- KeyEquivFilter (DialogPtr dlog, EventRecord* event, short* item)
- {
- if (event->what == keyDown) {
- UInt8 modifiers = (event->modifiers & kAllModifiers) >> kShiftModifiers;
- bool shift = false;
- do {
- shift = !shift;
- uchar ch = Modify(*event, shift);
-
- if (ch == 0x0D || ch == 0x03) { // If return or enter...
- *item = 1;
- return true;
- }
-
- for (const TKeyEquiv* ke = *TKeyEquivHandle(DialogPeek(dlog)->window.refCon);
- ke->fKey != 0; ++ke) {
- if (ch == ke->fKey && (modifiers & ke->fModsOn) == ke->fModsOn &&
- (modifiers & ke->fModsOff) == 0) {
- ClickItem(dlog, *item = ke->fItem);
- return true;
- }
- }
- } while (shift);
- }
-
- #ifdef _BBEditExtention_
- return BBEdit->StandardFilter(dlog, event, item);
- #else
- return false;
- #endif
- }
-
-
- void
- InitKeyEquiv (DialogPtr dlog, short rsrcID)
- {
- DialogPeek(dlog)->window.refCon = long(GetResource('DKey', rsrcID));
- }
-
-
- void
- EndKeyEquiv (DialogPtr dlog)
- {
- Handle& ke = Handle(DialogPeek(dlog)->window.refCon);
-
- if (ke != nil) {
- ReleaseResource(ke);
- ke = nil;
- }
- }
-